package com.station.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassMethod: 公共方法类
 * @Description: 公共方法类
 * @author 李祥
 * @version V1.0
 */
public class CommonUtil {

	/**
	 * 使用String的split 方法
	 * 
	 * @param str
	 * @param var
	 * @return
	 */
	public static String[] convertStrToArray(String str, String var) {
		String[] strArray = null;
		strArray = str.split(var); // 拆分字符为"," ,然后把结果交给数组strArray
		return strArray;
	}

	/**
	 * 判断str是否为数字的方法
	 * 
	 * @param str
	 * @return boolean
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 判断str是否为数字的方法
	 *
	 * @param str
	 * @return boolean
	 */
	public static boolean isNumeric2(String str) {
		Pattern pattern = Pattern.compile("-?[0-9]+.*[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 使用java正则表达式去掉多余的.与0
	 * 
	 * @param s
	 * @return
	 */
	public static String subZeroAndDot(String s) {
		if (s.indexOf(".") > 0) {
			s = s.replaceAll("0+?$", "");// 去掉多余的0
			s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
		}
		return s;
	}

	/**
	 * 获取IP地址
	 * 
	 * @return
	 */
	public static String getIpAddress() {
		// 获取request
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		// 设置IP地址
		String ipaddress = IPUtils.getIpAddr(request);
		return ipaddress;
	}

	/**
	 * 反射类的属性方法
	 * 
	 * @param str
	 * @return boolean
	 */
	public static StringBuffer reflexAttribute(Object model) {
		StringBuffer sBuffer = new StringBuffer();
		Field[] field = model.getClass().getDeclaredFields();
		for (int j = 0; j < field.length; j++) {
			String name = field[j].getName();
			System.out.println("attribute name:" + name);
			name = name.substring(0, 1).toUpperCase() + name.substring(1);
			String type = field[j].getGenericType().toString();
			if (type.equals("class java.lang.String")) {
				Method m;
				String value;
				try {
					m = model.getClass().getMethod("get" + name);
					value = (String) m.invoke(model);
					if (value != null) {
						sBuffer.append(name + ":" + value + ",");
					}
				} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
						| InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return sBuffer.deleteCharAt(sBuffer.length() - 1);
	}
	
	/**
	 * 是否为正整数
	 * @param number
	 * @return
	 */
	public static boolean isPositiveInteger(String number) {
		if( null == number || "".equals(number)) {
			return false;
		}
		return number.matches("[0-9]+");
    }
	
	/**
	 * 检验日期格式是否正确
	 * @param dateStr 日期字符串
	 * @return
	 */
	public static boolean isValidDate(String dateStr) {
		boolean convertSuccess=true;
	 	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	 	try {
	 		format.setLenient(false);
	 		format.parse(dateStr);
	 	} catch (ParseException e) {
	 		convertSuccess=false;
	 	} 
		return convertSuccess;
	}
	
	/**
	 * 生成随机数字和字母
	 * @param length
	 * @return
	 */
    public static String getStringRandom(int length) {  
        String val = "";  
        Random random = new Random();        
        for(int i = 0; i < length; i++) {          
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  
            if( "char".equalsIgnoreCase(charOrNum) ) {  
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
                val += (char)(random.nextInt(26) + temp);  
            } else if( "num".equalsIgnoreCase(charOrNum) ) {  
                val += String.valueOf(random.nextInt(10));  
            }  
        }  
        return val;  
    }  
    
    /**
     * 判读字符是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {  
    	if( null==str || "".equals(str) || "null".equals(str) ) {
    		return true;
    	}else{
    		return false;
    	}
    }
    
    /**
     * 判断是否为数字
     * @param str
     * @return
     */
    public static boolean isDigital(String str) { 
    	byte[] bytestr = str.getBytes();  
    	for( int i=0;i<bytestr.length;i++ ) {
            if (bytestr[i] < 48 || bytestr[i] > 57) {
            	return false;
            }
    	}
    	return true;
    }
    
    /** 
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数 
     * 此方法中前三位格式有： 
     * 13+任意数 
     * 15+除4的任意数 
     * 18+除1和4的任意数 
     * 17+除9的任意数 
     * 147 
     */  
    public static boolean isChinaPhoneLegal(String str){  
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";  
        Pattern p = Pattern.compile(regExp);  
        Matcher m = p.matcher(str);  
        return m.matches();  
    }  
    

}
